<?php
/**
 * @Created on 2020/01/15
 * @Author QQ 1026269609@qq.com
*/
namespace app\admin\controller;
use think\Controller;

class Login extends Controller
{
    public function index(){//登录页
    	if(session('uid')){
			$this->redirect('Index/index');
		}
        return $this->fetch();
    }
    public function login(){//登录验证

    	if(request()->isPost()){

    		$username=request()->param('username');//用户名
			$password=request()->param('password');//密码
			$captcha=request()->param('captcha');//验证码
			$login_ip=request()->ip();//访问ip
			//if(!captcha_check($captcha)){
				//return json(['status'=>0,'message'=>'请点击验证码后，重新输入验证码']);
			//};
			$user=db('member')->where(['username'=>$username,'password'=>md5($password)])->find();
			if($user){
				session('uid',$user['uid']);
				session('username',$user['username']);
				session('roleid',$user['roleid']);
				session('login_ip',$login_ip);
				session('login_time',date("Y-m-d H:i:s",time()));


				//添加登录日志
				$data=['uid'=>session('uid'),'username'=>session('username'),'roleid'=>session('roleid'),'login_ip'=>session('login_ip'),'login_time'=>session('login_time'),'remark'=>'登录后台'];
				db('member_login_log')->insert($data);
				return json(['status'=>1,'message'=>'登录成功']);
			}else{
				$true=db('member')->where(['username'=>$username])->find();
				if($true){
					return json(['status'=>0,'message'=>'密码有误']);
				}else{
					return json(['status'=>0,'message'=>'用户名不存在']);
				}
			}
    	}else{
    		$this->redirect('Login/index');
    	}
    }
    public function captcha(){//验证码
        $captcha = new \think\captcha\Captcha();
        $captcha->imageW=340;
        $captcha->imageH = 50;  //图片高
        $captcha->fontSize =18;  //字体大小
        $captcha->length   = 6;  //字符数
        $captcha->fontttf = '4.ttf';  //字体
        $captcha->expire = 30;  //有效期
        $captcha->useNoise = false;  //不添加杂点
        return $captcha->entry();
    }
    public function logout(){//安全退出
		session(null);
		$this->redirect('Login/index');
	}
}
